 /**
 * @author Burcak Otlu Saritas
 *
 * 
 */

package calculations;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import annotate.encode.Commons;

public class CalculateNumberofNonOverlappingBasePairs {
	
	public static Integer WINDOW_SIZE = new Integer(10000); 
	public static String EMPTY_STRING = ""; 	
	
	
	public void getHg19ChromosomeSizes(List<Integer> chromosomeSizes, String inputFileName){
		String strLine = null;
		String chromName = null;
		int indexofFirstTab = 0;
		int chromSize = 0;
		
		FileReader fileReader;
		BufferedReader bufferedReader;
		
		try {
			fileReader = new FileReader(inputFileName);
			bufferedReader = new BufferedReader(fileReader);
			
			while((strLine = bufferedReader.readLine())!= null){
				indexofFirstTab = strLine.indexOf('\t');
				
				if (indexofFirstTab >= 0){
					chromName = strLine.substring(0, indexofFirstTab);
					chromSize = Integer.valueOf(strLine.substring(indexofFirstTab+1));
				}
				
				if (Commons.CHROMOSOME1.equals(chromName)){
					chromosomeSizes.set(0, chromSize);
				}else if (Commons.CHROMOSOME2.equals(chromName)){
					chromosomeSizes.set(1, chromSize);
				}else if (Commons.CHROMOSOME3.equals(chromName)){
					chromosomeSizes.set(2, chromSize);
				}else if (Commons.CHROMOSOME4.equals(chromName)){
					chromosomeSizes.set(3, chromSize);
				}else if (Commons.CHROMOSOME5.equals(chromName)){
					chromosomeSizes.set(4, chromSize);
				}else if (Commons.CHROMOSOME6.equals(chromName)){
					chromosomeSizes.set(5, chromSize);
				}else if (Commons.CHROMOSOME7.equals(chromName)){
					chromosomeSizes.set(6, chromSize);
				}else if (Commons.CHROMOSOME8.equals(chromName)){
					chromosomeSizes.set(7, chromSize);
				}else if (Commons.CHROMOSOME9.equals(chromName)){
					chromosomeSizes.set(8, chromSize);
				}else if (Commons.CHROMOSOME10.equals(chromName)){
					chromosomeSizes.set(9, chromSize);
				}else if (Commons.CHROMOSOME11.equals(chromName)){
					chromosomeSizes.set(10, chromSize);
				}else if (Commons.CHROMOSOME12.equals(chromName)){
					chromosomeSizes.set(11, chromSize);
				}else if (Commons.CHROMOSOME13.equals(chromName)){
					chromosomeSizes.set(12, chromSize);
				}else if (Commons.CHROMOSOME14.equals(chromName)){
					chromosomeSizes.set(13, chromSize);
				}else if (Commons.CHROMOSOME15.equals(chromName)){
					chromosomeSizes.set(14, chromSize);
				}else if (Commons.CHROMOSOME16.equals(chromName)){
					chromosomeSizes.set(15, chromSize);
				}else if (Commons.CHROMOSOME17.equals(chromName)){
					chromosomeSizes.set(16, chromSize);
				}else if (Commons.CHROMOSOME18.equals(chromName)){
					chromosomeSizes.set(17, chromSize);
				}else if (Commons.CHROMOSOME19.equals(chromName)){
					chromosomeSizes.set(18, chromSize);
				}else if (Commons.CHROMOSOME20.equals(chromName)){
					chromosomeSizes.set(19, chromSize);
				}else if (Commons.CHROMOSOME21.equals(chromName)){
					chromosomeSizes.set(20, chromSize);
				}else if (Commons.CHROMOSOME22.equals(chromName)){
					chromosomeSizes.set(21, chromSize);
				}else if (Commons.CHROMOSOMEX.equals(chromName)){
					chromosomeSizes.set(22, chromSize);
				}else if (Commons.CHROMOSOMEY.equals(chromName)){
					chromosomeSizes.set(23, chromSize);
				}				
			} // End of While
			
			bufferedReader.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		 
		
	}
	
	
	public void printChromosomeSizes(List<Integer> chromosomeSizes){
		System.out.println("For information : Chromosome Sizes");
		
		for(int i =0; i<chromosomeSizes.size(); i++){
			if (i == 22)
				System.out.printf("ChrX \t %d\n", chromosomeSizes.get(i));
			else if (i == 23)
				System.out.printf("ChrY \t %d\n", chromosomeSizes.get(i));
			else
				System.out.printf("Chr%d \t %d\n", i+1, chromosomeSizes.get(i));
				
		}
	}

	public void initializeChromosomeSizes(List<Integer> chromosomeSizes){
		for(int i =0; i<Commons.NUMBER_OF_CHROMOSOMES_HG19; i++){
			chromosomeSizes.add(i, 0);
		}
	}

	public void initializeChromBaseList(List<List<String>> chromBaseWindowList,int windowSize){
		for(int i =0; i<windowSize; i++){
			chromBaseWindowList.add(i,new ArrayList<String>());
		}		
	}
	
	public boolean overlaps(int low_x, int high_x, int low_y, int high_y ){
		if (( low_x <= high_y) && (low_y <= high_x))
			return true;
		else 
			return false;
	}
	
	public int maximum(int x, int y){
		
		int max = x;
	
		if (y>max) 
			max = y;
		
		return max;
	}

	public int minimum(int x, int y){
		
		int min = x;
	
		if (y<min) 
			min = y;
		
		return min;
	}

	public void calculateNumberofNonoverlappingBasePairs(int chromosomeNumber, List<Integer> chromosomeSizes, Map<String,Long> dnaseCellLineNameHashMap,String inputFileName){
		
		FileReader fileReader;
		BufferedReader bufferedReader;
		String strLine = null;
		
		int indexofFirstTab = 0;
		int indexofSecondTab = 0;
		int indexofThirdTab = 0;
		int indexofFourthTab = 0;
		
		int low = 0;
		int high = 0;
		String cellLineName = null;
		String name;
		
		List<String> temp;
		
		boolean unprocessedInputLineExists = false;
		
		System.out.printf("chromosome%d\n" , chromosomeNumber+1);
		
		
		try {
			fileReader = new FileReader(inputFileName);								
			bufferedReader = new BufferedReader(fileReader);
			
				//how many windows do I need to use?
				int numberofWindows = chromosomeSizes.get(chromosomeNumber)/WINDOW_SIZE;
				
				for(int jthWindow = 0; jthWindow <=numberofWindows; jthWindow++){
					
//					System.out.println("jth\t" + jthWindow + "\twindow");
															
					List<List<String>> windowSizeList = new ArrayList<List<String>>(WINDOW_SIZE);
					initializeChromBaseList(windowSizeList,WINDOW_SIZE);
					
					int allowedWindowLow = jthWindow * WINDOW_SIZE;
					int allowedWindowHigh = allowedWindowLow + 9999; 
					
					
					while(unprocessedInputLineExists){
						if(overlaps(low,high,allowedWindowLow,allowedWindowHigh)){
							for(int j = maximum(low,allowedWindowLow); j <= minimum(high,allowedWindowHigh); j++ ){	
								temp = windowSizeList.get(j-(jthWindow*WINDOW_SIZE));
								if (!(temp.contains(cellLineName))){
										temp.add(cellLineName);
								}
								windowSizeList.set(j-(jthWindow*WINDOW_SIZE), temp);						
							}
							unprocessedInputLineExists = false;
						}else{
							break;
						}
					}
					
					if(!unprocessedInputLineExists)
						strLine = bufferedReader.readLine();
					
					while(!unprocessedInputLineExists && (strLine!= null)){
							
							//chr1	91852781	91853156	GM12878	idrPool.GM12878-DS9432-DS10671.z_OV_GM12878-DS10671.z_VS_GM12878-DS9432.z.npk2.narrowPeak
							indexofFirstTab = strLine.indexOf('\t');
							indexofSecondTab = strLine.indexOf('\t',indexofFirstTab+1);
							indexofThirdTab = strLine.indexOf('\t',indexofSecondTab+1);
							indexofFourthTab = strLine.indexOf('\t',indexofThirdTab+1);
							
							low = Integer.valueOf(strLine.substring(indexofFirstTab+1, indexofSecondTab));
							high =Integer.valueOf(strLine.substring(indexofSecondTab+1, indexofThirdTab));
//							cellLineName = strLine.substring(indexofThirdTab+1, indexofFourthTab).toUpperCase(Locale.ENGLISH);										
							cellLineName = strLine.substring(indexofThirdTab+1, indexofFourthTab);										
														
							if (low > allowedWindowHigh){
								unprocessedInputLineExists = true;
								break;
							}
															
							if(overlaps(low,high,allowedWindowLow,allowedWindowHigh)){
								for(int j = maximum(low,allowedWindowLow); j <= minimum(high,allowedWindowHigh); j++ ){	
									temp = windowSizeList.get(j-(jthWindow*WINDOW_SIZE));
									if (!(temp.contains(cellLineName))){										
										temp.add(cellLineName);
									}
									windowSizeList.set(j-(jthWindow*WINDOW_SIZE), temp);						
								}									
							}
												
							strLine = bufferedReader.readLine();
							
					}// end of while there is no unprocesses input line exists
						
					//increment the counts
					for(int k =0; k<windowSizeList.size(); k++ ){
						List<String> list = windowSizeList.get(k);
						
						if(list.size()>0){
							for(int l = 0 ; l< list.size(); l++){
								name = list.get(l);
								dnaseCellLineNameHashMap.put(name, (dnaseCellLineNameHashMap.get(name))+1);
							}																				
						}												
					}// end of for each existing dnase cell line name calculations	
				
			
				windowSizeList.clear();
				windowSizeList = null;
				 
				//input file has been consumed				
				if (strLine==null)
					break;
																	
			}// End of for each window size						
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	
		
	}
	
	public void fillChromBaseListforDnase(int chromosomeNumber, List<Integer> chromosomeSizes, Map<String,Long> dnaseCellLineNameHashMap){
	
		
			switch(chromosomeNumber){
				case 0: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR1_DNASE);
						break;
				case 1: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR2_DNASE);
						break;
				case 2: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR3_DNASE);
						break;
				case 3: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR4_DNASE);
						break;
				case 4: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR5_DNASE);
						break;
				case 5: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR6_DNASE);
						break;
				case 6: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR7_DNASE);
						break;
				case 7: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR8_DNASE);
						break;
				case 8: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR9_DNASE);
						break;
				case 9: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR10_DNASE);
						break;
				case 10: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR11_DNASE);
						break;
				case 11: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR12_DNASE);
						break;
				case 12: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR13_DNASE);
						break;
				case 13: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR14_DNASE);
						break;
				case 14: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR15_DNASE);
						break;
				case 15: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR16_DNASE);
						break;
				case 16: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR17_DNASE);
						break;
				case 17: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR18_DNASE);
						break;
				case 18: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR19_DNASE);
						break;
				case 19: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR20_DNASE);
						break;
				case 20: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR21_DNASE);
						break;
				case 21: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR22_DNASE);
						break;
				case 22: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHRX_DNASE);
						break;			
				case 23: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, dnaseCellLineNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHRY_DNASE);
						break;
								
			}//End of switch
					
	}
	
	
	
	public void fillChromBaseListforTfbs(int chromosomeNumber, List<Integer> chromosomeSizes, Map<String,Long> tfbsNameHashMap){
		
		
		switch(chromosomeNumber){
			case 0: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR1_TFBS);
					break;
			case 1: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR2_TFBS);
					break;
			case 2: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR3_TFBS);
					break;
			case 3: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR4_TFBS);
					break;
			case 4: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR5_TFBS);
					break;
			case 5: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR6_TFBS);
					break;
			case 6: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR7_TFBS);
					break;
			case 7: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR8_TFBS);
					break;
			case 8: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR9_TFBS);
					break;
			case 9: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR10_TFBS);
					break;
			case 10: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR11_TFBS);
					break;
			case 11: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR12_TFBS);
					break;
			case 12: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR13_TFBS);
					break;
			case 13: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR14_TFBS);
					break;
			case 14: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR15_TFBS);
					break;
			case 15: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR16_TFBS);
					break;
			case 16: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR17_TFBS);
					break;
			case 17: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR18_TFBS);
					break;
			case 18: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR19_TFBS);
					break;
			case 19: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR20_TFBS);
					break;
			case 20: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR21_TFBS);
					break;
			case 21: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR22_TFBS);
					break;
			case 22: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHRX_TFBS);
					break;
			case 23: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, tfbsNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHRY_TFBS);
					break;
				
		}//End of switch
				
	}

	
	public void fillChromBaseListforHistone(int chromosomeNumber, List<Integer> chromosomeSizes, Map<String,Long> histoneNameHashMap){
		
		
		switch(chromosomeNumber){
			case 0: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR1_HISTONE);
					break;
			case 1: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR2_HISTONE);
			break;
			case 2: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR3_HISTONE);
			break;
			case 3: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR4_HISTONE);
			break;
			case 4: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR5_HISTONE);
			break;
			case 5: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR6_HISTONE);
			break;
			case 6: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR7_HISTONE);
			break;
			case 7: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR8_HISTONE);
			break;
			case 8: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR9_HISTONE);
			break;
			case 9: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR10_HISTONE);
			break;
			case 10: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR11_HISTONE);
			break;
			case 11: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR12_HISTONE);
			break;
			case 12: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR13_HISTONE);
			break;
			case 13: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR14_HISTONE);
			break;
			case 14: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR15_HISTONE);
			break;
			case 15: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR16_HISTONE);
			break;
			case 16: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR17_HISTONE);
			break;
			case 17: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR18_HISTONE);
			break;
			case 18: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR19_HISTONE);
			break;
			case 19: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR20_HISTONE);
			break;
			case 20: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR21_HISTONE);
			break;
			case 21: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHR22_HISTONE);
			break;
			case 22: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHRX_HISTONE);
			break;
			case 23: calculateNumberofNonoverlappingBasePairs(chromosomeNumber, chromosomeSizes, histoneNameHashMap, Commons.C_ECLIPSE_GANYMEDE_WORKSPACE_DOKTORA_ANNOTATE_ENCODE_SORTED_CHRY_HISTONE);
			break;
							
		}//End of switch
				
	}
	
	
	public void calculateNumberofNonoverlappingBasePairsforDnase(List<Integer> chromosomeSizes,Map<String,Long> dnaseCellLineNameHashMap){				
		
		for(int i= 0; i<Commons.NUMBER_OF_CHROMOSOMES_HG19; i++){
			fillChromBaseListforDnase(i,chromosomeSizes,dnaseCellLineNameHashMap);																		
		} // End of For each chromosome in the genome
		
	}
	
	
	public void calculateNumberofNonoverlappingBasePairsforTfbs(List<Integer> chromosomeSizes,Map<String,Long> tfbsNameHashMap){				
		
		for(int i= 0; i<Commons.NUMBER_OF_CHROMOSOMES_HG19; i++){
			fillChromBaseListforTfbs(i,chromosomeSizes,tfbsNameHashMap);																		
		} // End of For each chromosome in the genome
		
	}


	public void calculateNumberofNonoverlappingBasePairsforHistone(List<Integer> chromosomeSizes,Map<String,Long> tfbsNameHashMap){				
	
		for(int i= 0; i<Commons.NUMBER_OF_CHROMOSOMES_HG19; i++){
			fillChromBaseListforHistone(i,chromosomeSizes,tfbsNameHashMap);																		
		} // End of For each chromosome in the genome
		
	}

	public void fillHashMapwithZeros(Map<String,Long> hashMap, String inputFileName){
		String strLine;
		FileReader fileReader = null;
		BufferedReader bufferedReader = null;
		
		try {
			fileReader = new FileReader(inputFileName);			
			bufferedReader = new BufferedReader(fileReader);
			
			while((strLine = bufferedReader.readLine())!=null) {
				if (!(hashMap.containsKey(strLine))){
//					hashMap.put(strLine.toUpperCase(Locale.ENGLISH), Commons.LONG_ZERO);
					hashMap.put(strLine, Commons.LONG_ZERO);
				}
				
				strLine = null;
			}
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			bufferedReader.close();
			fileReader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}			
	}
	
	

	
	public void writeNumberofOccurencesonNonoverlappingBasePairs(Map<String,Long> dnaseCellLineNameHashMap, String outputFileName){
		FileWriter fileWriter;
		BufferedWriter bufferedWriter;
		
		try {
			fileWriter = new FileWriter(outputFileName);
			bufferedWriter = new BufferedWriter(fileWriter);
			
			Set<Map.Entry<String,Long>> set =dnaseCellLineNameHashMap.entrySet();
			
			Iterator<Map.Entry<String,Long>> iterator = set.iterator();
			
			while(iterator.hasNext()){
				Map.Entry<String,Long> entry = iterator.next();	
				bufferedWriter.write(entry.getKey()+ "\t" + entry.getValue() + "\n");
			}

			bufferedWriter.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	
	
	
	public static void main(String[] args) {
		
		
		List<Integer> chromosomeSizes = new ArrayList<Integer>(24);
		
		CalculateNumberofNonOverlappingBasePairs calculate = new CalculateNumberofNonOverlappingBasePairs();
		
		calculate.initializeChromosomeSizes(chromosomeSizes);
		calculate.getHg19ChromosomeSizes(chromosomeSizes,Commons.HG19_CHROMOSOME_SIZES_INPUT_FILE);
		calculate.printChromosomeSizes(chromosomeSizes);
				
		Map<String,Long> dnaseCellLineNumberofNonoverlappingOccurrencesWholeGenomeHashMap = new HashMap<String,Long>();
		Map<String,Long> tfbsNumberofNonoverlappingOccurrencesWholeGenomeHashMap = new HashMap<String,Long>();
		Map<String,Long> histoneNumberofNonoverlappingOccurrencesWholeGenomeHashMap = new HashMap<String,Long>();
		
		calculate.fillHashMapwithZeros(dnaseCellLineNumberofNonoverlappingOccurrencesWholeGenomeHashMap,Commons.WRITE_ALL_POSSIBLE_DNASE_CELL_NAMES_OUTPUT_FILE);
		calculate.fillHashMapwithZeros(tfbsNumberofNonoverlappingOccurrencesWholeGenomeHashMap,Commons.WRITE_ALL_POSSIBLE_TFBS_NAMES_OUTPUT_FILE);
		calculate.fillHashMapwithZeros(histoneNumberofNonoverlappingOccurrencesWholeGenomeHashMap,Commons.WRITE_ALL_POSSIBLE_HISTONE_NAMES_OUTPUT_FILE);
		
		calculate.calculateNumberofNonoverlappingBasePairsforDnase(chromosomeSizes,dnaseCellLineNumberofNonoverlappingOccurrencesWholeGenomeHashMap);
		calculate.writeNumberofOccurencesonNonoverlappingBasePairs(dnaseCellLineNumberofNonoverlappingOccurrencesWholeGenomeHashMap,Commons.NUMBER_OF_OCCURRENCES_OF_EACH_DNASE_CELL_LINE_WHOLE_GENOME_OUTPUT_FILE);

		calculate.calculateNumberofNonoverlappingBasePairsforTfbs(chromosomeSizes,tfbsNumberofNonoverlappingOccurrencesWholeGenomeHashMap);		
		calculate.writeNumberofOccurencesonNonoverlappingBasePairs(tfbsNumberofNonoverlappingOccurrencesWholeGenomeHashMap,Commons.NUMBER_OF_OCCURRENCES_OF_EACH_TFBS_WHOLE_GENOME_OUTPUT_FILE);

		calculate.calculateNumberofNonoverlappingBasePairsforHistone(chromosomeSizes,histoneNumberofNonoverlappingOccurrencesWholeGenomeHashMap);						
		calculate.writeNumberofOccurencesonNonoverlappingBasePairs(histoneNumberofNonoverlappingOccurrencesWholeGenomeHashMap,Commons.NUMBER_OF_OCCURRENCES_OF_EACH_HISTONE_WHOLE_GENOME_OUTPUT_FILE);
		
	}

}
